﻿using System;
using System.IO;
using System.Windows.Forms;
using System.Collections.Generic;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Threading;
using System.Xml.Serialization;

namespace MapEditor
{
    public partial class Form1 : Form
    {
        Cursor          cursor;
        KeyboardState   keyboardState = Keyboard.GetState();
        ListView[]      listViewTab;
        ImageList[]     imageListTab;

        //variables temporaire
        private Module  module;
        private MapData     map;

        #region initialisation

        public Form1()
        {
            Log.info("application started------------------------------------------------------------");
            InitializeComponent();
            myDisplay1.onInitialise += new EventHandler(myDisplay1_onInitialise);
            myDisplay1.onDraw += new EventHandler(myDisplay1_onDraw);
            Application.Idle += delegate { myDisplay1.Invalidate(); };

            //init listViewTab
            listViewTab = new ListView[4];
            listViewTab[0] = listViewFG;
            listViewTab[1] = listViewO;
            listViewTab[2] = listViewBG;
            listViewTab[3] = listViewC;

            //init imageListTab
            imageListTab = new ImageList[(int)Decor._SIZE_ * 2];
            for (int i = 0; i < imageListTab.GetLength(0); i++)
            {
                imageListTab[i] = new ImageList(this.components);
                imageListTab[i].ImageSize = new System.Drawing.Size(64, 64);
                imageListTab[i].TransparentColor = System.Drawing.Color.White;
            }
            foreach (ListView lv in listViewTab)
                lv.LargeImageList = imageListTab[0];
        }

        /// <summary>
        /// initialisation de la fenétre XNA.
        /// </summary>
        void myDisplay1_onInitialise(object sender, EventArgs e)
        {
            Utils.spritebatch = new SpriteBatch(myDisplay1.GraphicsDevice);

            //init Utils.tiles
            Utils.tiles = new TilesTexture(Utils.spritebatch, myDisplay1.GraphicsDevice, imageListTab, listViewTab);

            //init map
            MapData.list = new List<MapData>();

            //init scrollbar
            hScrollBar1.Visible = false;
            vScrollBar1.Visible = false;


            //init cursor
            Mouse.WindowHandle = myDisplay1.Handle;
            cursor = new Cursor(Utils.tiles, Utils.spritebatch, myDisplay1.GraphicsDevice);

            //init copier-coller class
            CopyPaste.Singleton = new CopyPaste(myDisplay1.GraphicsDevice, Utils.tiles, Utils.spritebatch);
        }

        #endregion

        #region menu/edition
        /// <summary>
        /// edition/zoom +.
        /// </summary>
        private void zoomToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Utils.tiles.Width =  (int)(Utils.tiles.Width  * 1.2f);
            Utils.tiles.Height = (int)(Utils.tiles.Height * 1.2f);
            hScrollBar1.LargeChange = myDisplay1.Size.Width / Utils.tiles.Width;
            vScrollBar1.LargeChange = myDisplay1.Size.Height / Utils.tiles.Height;
        }

        /// <summary>
        /// edition/zoom -.
        /// </summary>
        private void zoomToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Utils.tiles.Width =  (int)(Utils.tiles.Width  / 1.2f);
            Utils.tiles.Height = (int)(Utils.tiles.Height / 1.2f);
            hScrollBar1.LargeChange = myDisplay1.Size.Width / Utils.tiles.Width;
            vScrollBar1.LargeChange = myDisplay1.Size.Height / Utils.tiles.Height;
        }

        /// <summary>
        /// edition/annuler.
        /// </summary>
        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Undo.Singleton.undo();
        }

        /// <summary>
        /// edition/rétablir.
        /// </summary>
        private void rétablirToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Undo.Singleton.redo();
        }

        /// <summary>
        /// edition/Copier.
        /// </summary>
        private void toolStripMenuItem4_Click(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode != null &&
                (string)treeView1.SelectedNode.Tag == "module")
            {
                module = MapData.getMapByName(treeView1.SelectedNode.Parent.Text).getModule(treeView1.SelectedNode.Text);
                CopyPaste.Singleton.saveSelection(module);
            }
            else if (treeView1.SelectedNode != null &&
                (string)treeView1.SelectedNode.Tag == "mapplat")
            {
                module = MapData.getMapByName(treeView1.SelectedNode.Text).getModule();
                CopyPaste.Singleton.saveSelection(module);
            }
        }

        /// <summary>
        /// edition/Coller.
        /// </summary>
        private void toolStripMenuItem5_Click(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode != null &&
                (string)treeView1.SelectedNode.Tag == "module")
            {
                module = MapData.getMapByName(treeView1.SelectedNode.Parent.Text).getModule(treeView1.SelectedNode.Text);
                CopyPaste.Singleton.paste(module, 
                                          new Vector2I(Mouse.GetState().Y / Utils.tiles.Height + vScrollBar1.Value,
                                                       Mouse.GetState().X / Utils.tiles.Width + hScrollBar1.Value));
            }
            else if (treeView1.SelectedNode != null &&
                     (string)treeView1.SelectedNode.Tag == "mapplat")
            {
                module = MapData.getMapByName(treeView1.SelectedNode.Text).getModule();
                CopyPaste.Singleton.paste(module,
                                          new Vector2I(Mouse.GetState().Y / Utils.tiles.Height + vScrollBar1.Value,
                                                       Mouse.GetState().X / Utils.tiles.Width + hScrollBar1.Value));
            }
        }
        #endregion

        #region menu/fichier

        /// <summary>
        /// Fichier/quitter.
        /// </summary>
        private void quitterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Log.info("application exit");
            Application.Exit();
        }

        /// <summary>
        /// fichier/nouveau.
        /// </summary>
        private void nouveauToolStripMenuItem_Click(object sender, EventArgs e)
        {
            clickevent_node_addMap(null, null);
        }
        #endregion

        #region treeview

        /// <summary>
        /// clic sur une CheckBox. edit les propriété du module séléctionné.
        /// </summary>
        private void checkBox_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox c = (CheckBox)sender;
            Module m;
            try { m = MapData.getMapByName(treeView1.SelectedNode.Parent.Text).getModule(treeView1.SelectedNode.Text); }
            catch { Log.error("checkBox_CheckedChanged invalid:" + sender.ToString() + ", " + e.ToString()); return; }
            if (c.Name == "checkBoxN")
                m.north = c.Checked;
            else if (c.Name == "checkBoxO")
                m.west = c.Checked;
            else if (c.Name == "checkBoxE")
                m.east = c.Checked;
            else if (c.Name == "checkBoxS")
                m.south = c.Checked;
            else if (c.Name == "checkBoxStart")
                m.start = c.Checked;
            else if (c.Name == "checkBoxEnd")
                m.end = c.Checked;
        }

        /// <summary>
        /// appelé en cas de clique sur une node du treeview.
        /// </summary>
        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            // bouton gauche
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
            }
            // bouton droit
            else if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                treeView1.SelectedNode = e.Node;
                contextMenuStrip1.Visible = true;
                contextMenuStrip1.Left = Button.MousePosition.X;
                contextMenuStrip1.Top = Button.MousePosition.Y;
            }
        }

        private String lastName; //stock le nom avant l'edition
        /// <summary>
        /// clic droit sur node/renommer
        /// </summary>
        private void clickevent_node_rename(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode != null)
            {
                lastName = treeView1.SelectedNode.Text;
                treeView1.SelectedNode.BeginEdit();
            }
        }

        /// <summary>
        /// edtion du nom d'une node dans le treeview.
        /// </summary>
        private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
        {
            if (e.Label == null) //aucune modification
                return;

            if ((string)e.Node.Tag == "module")
            {
                MapData m = MapData.getMapByName(e.Node.Parent.Text);
                if (m.checkModuleNames(e.Label))
                    e.CancelEdit = true;
                else
                    m.getModule(lastName).name = e.Label;
                return;
            }
            else
            {
                if (MapData.checkMapNames(e.Label))
                    e.CancelEdit = true;
                else
                    MapData.getMapByName(lastName).name = e.Label;
            }
        }

        /// <summary>
        /// clic droit sur node/monter.
        /// </summary>
        private void clickevent_node_up(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode == null)
                return;
            TreeNode node = treeView1.SelectedNode;
            if ((string)treeView1.SelectedNode.Tag == "module")
            {
                TreeNode nodeParent = node.Parent;
                int i = nodeParent.Nodes.IndexOf(node);
                if (i == 0) //return si premier de liste
                    return;
                nodeParent.Nodes.Remove(node);
                nodeParent.Nodes.Insert(i - 1, node);

                MapData.getMapByName(nodeParent.Text).up(node.Text);

                treeView1.SelectedNode = node;
            }
            else
            {
                int i = treeView1.Nodes.IndexOf(node);
                if (i == 0) //return si premier de liste
                    return;
                treeView1.Nodes.Remove(node);
                treeView1.Nodes.Insert(i - 1, node);

                MapData m = MapData.getMapByName(node.Text);
                i = MapData.list.IndexOf(m);
                MapData.list.Remove(m);
                MapData.list.Insert(i - 1, m);
            }
            treeView1.SelectedNode = node;
        }

        /// <summary>
        /// clic droit sur node/descendre.
        /// </summary>
        private void clickevent_node_down(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode == null)
                return;
            TreeNode node = treeView1.SelectedNode;
            if ((string)treeView1.SelectedNode.Tag == "module")
            {
                TreeNode nodeParent = node.Parent;
                int i = nodeParent.Nodes.IndexOf(node);
                if (i == nodeParent.Nodes.Count - 1) //return si dernier de liste
                    return;
                nodeParent.Nodes.Remove(node);
                nodeParent.Nodes.Insert(i + 1, node);

                MapData.getMapByName(nodeParent.Text).down(node.Text);
            }
            else
            {
                int i = treeView1.Nodes.IndexOf(node);
                if (i == treeView1.Nodes.Count - 1) //return si dernier de liste
                    return;
                treeView1.Nodes.Remove(node);
                treeView1.Nodes.Insert(i + 1, node);

                MapData m = MapData.getMapByName(node.Text);
                i = MapData.list.IndexOf(m);
                MapData.list.Remove(m);
                MapData.list.Insert(i + 1, m);
            }
            treeView1.SelectedNode = node;
        }

        FormNewMap f = new FormNewMap();
        /// <summary>
        /// clic droit sur node/ajouter map.
        /// </summary>
        private void clickevent_node_addMap(object sender, EventArgs e)
        {
            f = new FormNewMap();
            if (f.ShowDialog() == DialogResult.OK)
            {
                MapData m = new MapData(f, Utils.spritebatch, Utils.tiles);
                while (MapData.checkMapNames(m.name) == true)
                    m.name += "0";
                MapData.list.Add(m);
                TreeNode node = new TreeNode(m.name);
                node.Tag = (m.type == 1 ? "mapiso" : "mapplat");
                treeView1.Nodes.Add(node);
            }
        }

        private int countNewModules = 1;    //incrémente à chaque nouveau module pour eviter la creation de 2 module portant le même nom
        // TODO: à modifier une fois le chargement implémenté
        /// <summary>
        /// clic droit sur node/ajouter module.
        /// </summary>
        private void clickevent_node_addModule(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode == null)
                return;
            if ((string)treeView1.SelectedNode.Tag != "mapplat")
            {
                string moduleName = "newModule" + countNewModules++.ToString();
                TreeNode node = new TreeNode(moduleName);
                node.Tag = "module";

                if ((string)treeView1.SelectedNode.Tag == "mapiso")
                {
                    treeView1.SelectedNode.Nodes.Add(node);
                    MapData.getMapByName(treeView1.SelectedNode.Text).newModule(moduleName);
                }
                else if ((string)treeView1.SelectedNode.Tag == "module")
                {
                    treeView1.SelectedNode.Parent.Nodes.Add(node);
                    MapData.getMapByName(treeView1.SelectedNode.Parent.Text).newModule(moduleName);
                }
                treeView1.SelectedNode = node;
                node.BeginEdit();
                lastName = moduleName;
            }
        }

        /// <summary>
        /// treeview1, appelé avant un rename.
        /// </summary>
        private void treeView1_BeforeLabelEdit(object sender, NodeLabelEditEventArgs e)
        {
            lastName = treeView1.SelectedNode.Text;
        }

        /// <summary>
        /// click droit sur node/suprimer.
        /// </summary>
        private void clickevent_node_delete(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode == null)
                return;
            TreeNode node = treeView1.SelectedNode;
            if ((string)treeView1.SelectedNode.Tag == "module")
            {
                TreeNode nodeParent = node.Parent;
                nodeParent.Nodes.Remove(node);
                MapData.getMapByName(nodeParent.Text).delete(node.Text);
            }
            else
            {
                treeView1.Nodes.Remove(node);
                MapData m = MapData.getMapByName(node.Text);
                MapData.list.Remove(m);
            }
        }


        /// <summary>
        /// changement de selection dans le treeview. modifie l'apparence de l'onglet de droite et charge les valeurs.
        /// </summary>
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (treeView1.SelectedNode == null)
                return;

            if ((string)treeView1.SelectedNode.Tag == "module")
            {
                map = MapData.getMapByName(treeView1.SelectedNode.Parent.Text);
                module = map.getModule(treeView1.SelectedNode.Text);

                //checkbox en fonction du module
                checkBoxN.Enabled = true;
                checkBoxE.Enabled = true;
                checkBoxO.Enabled = true;
                checkBoxS.Enabled = true;
                checkBoxStart.Enabled = true;
                checkBoxEnd.Enabled = true;
                checkBoxN.Checked = module.north;
                checkBoxE.Checked = module.east;
                checkBoxO.Checked = module.west;
                checkBoxS.Checked = module.south;
                checkBoxStart.Checked = module.start;
                checkBoxEnd.Checked = module.end;

                //scrollBar en fonction de la taille du module
                hScrollBar1.Maximum = map.sizeX;
                vScrollBar1.Maximum = map.sizeY;
                hScrollBar1.Visible = true;
                vScrollBar1.Visible = true;
            }
            else
            {
                map = MapData.getMapByName(treeView1.SelectedNode.Text);

                //desactivation des checkbox, inutile pour une map
                checkBoxN.Enabled = false;
                checkBoxE.Enabled = false;
                checkBoxO.Enabled = false;
                checkBoxS.Enabled = false;
                checkBoxStart.Enabled = false;
                checkBoxEnd.Enabled = false;
                checkBoxN.Checked = false;
                checkBoxE.Checked = false;
                checkBoxO.Checked = false;
                checkBoxS.Checked = false;
                checkBoxStart.Checked = false;
                checkBoxEnd.Checked = false;

                //scrollBar 
                if ((string)treeView1.SelectedNode.Tag == "mapiso")
                {
                    hScrollBar1.Visible = false;
                    vScrollBar1.Visible = false;
                }
                else
                {
                    hScrollBar1.Visible = true;
                    vScrollBar1.Visible = true;
                    hScrollBar1.Maximum = map.sizeX;
                    vScrollBar1.Maximum = map.sizeY;
                }
            }
            //images en fonctions du décor et du type de la map
            Utils.tiles.CurrentDecor = map.decor;
            Utils.tiles.CurrentType = map.type;

            foreach (ListView lv in listViewTab)
            {
                if (map.type == 1)
                    lv.LargeImageList = imageListTab[(int)map.decor];
                else
                    lv.LargeImageList = imageListTab[(int)map.decor + (int)Decor._SIZE_];
            }
        }

        /// <summary>
        /// clic sur node/propriétés. Edit la map.
        /// </summary>
        private void propriétéToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode != null && treeView1.SelectedNode.Tag.ToString().Contains("map"))
            {
                MapData m = MapData.getMapByName(treeView1.SelectedNode.Text);
                f = new FormNewMap(m);
                if (f.ShowDialog() == DialogResult.OK)
                {
                    // nom changé
                    if (m.name != f.textBoxName.Text)
                    {
                        while (MapData.checkMapNames(m.name) == true)
                            m.name += "0";
                        treeView1.SelectedNode.Text = f.textBoxName.Text;
                        m.name = f.textBoxName.Text;
                    }

                    // decor changé
                    if ((int)m.decor != f.comboBoxDecor.SelectedIndex)
                    {
                        m.decor = (Decor)f.comboBoxDecor.SelectedIndex;
                        Utils.tiles.CurrentDecor = map.decor;
                        foreach (ListView lv in listViewTab)
                        {
                            if (m.type == 1)
                                lv.LargeImageList = imageListTab[(int)map.decor];
                            else
                                lv.LargeImageList = imageListTab[(int)map.decor + (int)Decor._SIZE_];
                        }
                    }

                    // taille changé
                    int x = int.Parse(f.textBoxSizeX.Text);
                    int y = int.Parse(f.textBoxSizeY.Text);
                    if (m.sizeX != x ||
                        m.sizeY != y)
                        m.resize(x, y);

                    // paramétre de génération
                    m.generationParameters[0] = f.comboBoxMazeType.SelectedIndex;
                    m.generationParameters[1] = f.trackBarFreq.Value;
                    m.generationParameters[2] = f.trackBarLengh.Value;
                    m.generationParameters[3] = Convert.ToInt32(f.textBoxMapLengh.Text);
                }
            }
        }

        #endregion

        #region fenétre XNA.

        /// <summary>
        /// boucle principale de la fenétre XNA.
        /// </summary>
        void myDisplay1_onDraw(object sender, EventArgs e)
        {
            //clear la fenetre.
            myDisplay1.GraphicsDevice.Clear(Color.Black);

            //input
            keyboardState = Keyboard.GetState();

            //clique sur la fenétre XNA.
            if (Mouse.GetState().LeftButton == Microsoft.Xna.Framework.Input.ButtonState.Pressed &&
                cursor.mouseOverMyDisplay)
                LeftClickOnMap();
            else if (Mouse.GetState().RightButton == Microsoft.Xna.Framework.Input.ButtonState.Pressed &&
                cursor.mouseOverMyDisplay)
                RightClickOnMap();

            //dessine le module selectionné.
            if (treeView1.SelectedNode != null && (string)treeView1.SelectedNode.Tag == "module")
                MapData.getMapByName(treeView1.SelectedNode.Parent.Text).getModule(treeView1.SelectedNode.Text).draw(tabControl1.SelectedIndex == 3, hScrollBar1.Value, vScrollBar1.Value);
            else if (treeView1.SelectedNode != null && (string)treeView1.SelectedNode.Tag == "mapplat")
                MapData.getMapByName(treeView1.SelectedNode.Text).getModule().draw(tabControl1.SelectedIndex == 3, hScrollBar1.Value, vScrollBar1.Value);
            //dessine le curseur.
            cursor.drawCursor(trackBarBrush.Value);

            //dessine la séléction
            CopyPaste.Singleton.drawSelection(hScrollBar1.Value, vScrollBar1.Value);

            //bride le framerate de la fenetre XNA à 60fps.
            FrameRate.limitFrameRate();
        }

        /// <summary>
        /// fonction apeller par delegate pour Utils.recursBrush 
        /// </summary>
        private void singleChangeTile(int y, int x)
        {
            module.changeTile(tabControl1.SelectedIndex,
                              x,
                              y,
                              int.Parse(listViewTab[tabControl1.SelectedIndex].SelectedItems[0].Tag.ToString())); //recupére l'id de la tile, stocké dans le tag de l'item du listview
        }

        /// <summary>
        /// clique droit sur une case.
        /// </summary>
        private void RightClickOnMap()
        {
            Vector2I v = new Vector2I(Mouse.GetState().Y / Utils.tiles.Height + vScrollBar1.Value,
                          Mouse.GetState().X / Utils.tiles.Width + hScrollBar1.Value);
            if (!keyboardState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.LeftShift))
                CopyPaste.Singleton.selected.Clear();
            else
                CopyPaste.Singleton.selected.Remove(v);
        }
        /// <summary>
        /// clique gauche sur une case.
        /// </summary>
        private void LeftClickOnMap()
        {
            if (trackBarBrush.Value > 0)
            {
                // edit la map
                if (treeView1.SelectedNode != null && (string)treeView1.SelectedNode.Tag == "module" && // vérifie qu'un module est séléctionné.
                    listViewTab[tabControl1.SelectedIndex].SelectedItems.Count == 1)                    // s'assure qu'exactement 1 element est selectionné.
                {
                    module = MapData.getMapByName(treeView1.SelectedNode.Parent.Text).getModule(treeView1.SelectedNode.Text); //stock le module séléctionné dans m
                    Utils.recursBrush(trackBarBrush.Value,
                                      new Vector2I(Mouse.GetState().X / Utils.tiles.Width + hScrollBar1.Value,
                                                   Mouse.GetState().Y / Utils.tiles.Height + vScrollBar1.Value),
                                      singleChangeTile);
                    Utils.resetRecursBrush();
                }
                else if (treeView1.SelectedNode != null && (string)treeView1.SelectedNode.Tag == "mapplat" && // vérifie qu'une map platform est séléctionné.
                    listViewTab[tabControl1.SelectedIndex].SelectedItems.Count == 1)                          // s'assure qu'exactement 1 element est selectionné.
                {
                    module = MapData.getMapByName(treeView1.SelectedNode.Text).getModule(); //stock le module séléctionné dans m
                    Utils.recursBrush(trackBarBrush.Value,
                                      new Vector2I(Mouse.GetState().X / Utils.tiles.Width + hScrollBar1.Value,
                                                   Mouse.GetState().Y / Utils.tiles.Height + vScrollBar1.Value),
                                      singleChangeTile);
                    Utils.resetRecursBrush();
                }
            }
            else
            {
                //modifie la selection
                Vector2I v = new Vector2I(Mouse.GetState().Y / Utils.tiles.Height + vScrollBar1.Value,
                                          Mouse.GetState().X / Utils.tiles.Width + hScrollBar1.Value);
                if (!keyboardState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.LeftShift))
                    CopyPaste.Singleton.selected.Clear();
                if (!CopyPaste.Singleton.selected.Contains(v))
                    CopyPaste.Singleton.selected.Add(v);
            }
        }

        /// <summary>
        /// souris entre au dessus de la fenétre XNA.
        /// </summary>
        private void myDisplay1_MouseEnter(object sender, EventArgs e)
        {
            cursor.mouseOverMyDisplay = true;
        }
        /// <summary>
        /// souris quitte la fenétre XNA.
        /// </summary>
        private void myDisplay1_MouseLeave(object sender, EventArgs e)
        {
            cursor.mouseOverMyDisplay = false;
        }

        #endregion

        #region sauvegarde et serialisation XML

        private void save(string path)
        {
            XmlSerializer xs = new XmlSerializer(typeof(List<MapData>));
            using (StreamWriter wr = new StreamWriter(path))
            {
                xs.Serialize(wr, MapData.list);
            }
        }

        private void load(string path)
        {
            MapData.load(path);

            //recrée l'arboressence du treeview
            treeView1.Nodes.Clear();
            foreach (MapData ma in MapData.list)
            {
                TreeNode mapNode = new TreeNode(ma.name);
                mapNode.Tag = (ma.type == 1 ? "mapiso" : "mapplat");
                foreach (Module mo in ma)
                {
                    TreeNode ModuleNode = new TreeNode(mo.name);
                    ModuleNode.Tag = "module";
                    mapNode.Nodes.Add(ModuleNode);
                }
                treeView1.Nodes.Add(mapNode);
            }
            //permet de sauvegarder directement sans réindiquer le path
            saveFileDialog1.FileName = path;
        }

        private void enregistrerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.FileName == "")
                saveFileDialog1.ShowDialog();
            else
                save(saveFileDialog1.FileName);
        }

        private void ouvrirToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
        }

        private void saveFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
        {
            save(saveFileDialog1.FileName);
        }

        private void enregistrersousToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFileDialog1.ShowDialog();
        }

        private void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
        {
            load(openFileDialog1.FileName);
        }

        #endregion

        /// <summary>
        /// resize du form.
        /// </summary>
        private void Form1_Resize(object sender, EventArgs e)
        {
            //update les scrollbar
            hScrollBar1.LargeChange = myDisplay1.Size.Width / Utils.tiles.Width;
            vScrollBar1.LargeChange = myDisplay1.Size.Height / Utils.tiles.Height;
        }
    }
}
